package com.bsj.travel.mqtt.factory;

import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.def.global.JsonResultEnum;
import com.bsj.travel.def.mqtt.message.SendMqttMsg;
import com.bsj.travel.mqtt.RemoteMqttBrokerService;
import com.bsj.travel.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author yinhao
 * @version 1.0
 * @description mqtt节点客户端降级处理
 * @date 2023/11/9
 */
@Component
@Slf4j
public class RemoteMqttBrokerFallbackFactory implements FallbackFactory<RemoteMqttBrokerService> {

    @Override
    public RemoteMqttBrokerService create(Throwable cause) {
        log.error("Mqtt Broker获取节点服务调用失败: {}", ExceptionUtil.getStackStr(cause));
        return new RemoteMqttBrokerService() {
            @Override
            public JsonResult queryNodes() {
                return JsonResult.fail(JsonResultEnum.QUERY_NODES_FAIL);
            }

            @Override
            public JsonResult queryOnlineClientSize() {
                return JsonResult.fail(JsonResultEnum.QUERY_ONLINE_CLIENT_SIZE_FAIL);
            }

            @Override
            public JsonResult queryNodeOnlineSize(String nodeName) {
                return JsonResult.fail(JsonResultEnum.QUERY_NODE_ONLINE_SIZE_FAIL);
            }

            @Override
            public JsonResult<Boolean> sendMsg(SendMqttMsg sendMqttMsg) {
                return JsonResult.fail(JsonResultEnum.SEND_MQTT_MSG_FAIL);
            }

            @Override
            public JsonResult<Boolean> mqttConnectTake(SendMqttMsg sendMqttMsg) {
                return JsonResult.fail(JsonResultEnum.MQTT_CONNECT_TAKE_FAIL);
            }

        };
    }
}
